const path = require('path')
const mode = process.env.NODE_ENV
const merge = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const envConfig = require(`./webpack.config.${mode}`)
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')
module.exports = env => {
  return merge({
    mode: mode,
    entry: {
      main: `./src/main.ts`
    },
    output: {
      path: path.resolve(__dirname, `../dist/${env.entry}`),
      filename: 'javascript/[name].[hash:6].js',
      publicPath: '/'
    },
    module: {
      rules: [
        {
          test: /\.vue$/,
          exclude: /node_modules/,
          loader: 'vue-loader'
        },
        {
          test: /\.ts$/,
          exclude: /node_modules|vue\/src/,
          use: [
            {loader: 'cache-loader'},
            {
              loader: 'thread-loader',
              options: {
                workers: require('os').cpus().length - 1
              }
            },
            {
              loader: 'ts-loader',
              options: {
                happyPackMode: true,
                appendTsSuffixTo: [/\.vue$/],
                transpileOnly: true
              }
            }
          ]
        },
        {
          test: /\.(png|jpg|gif)$/,
          use: [
            {
              loader: 'file-loader',
              options: {
                publicPath: 'https://hpx-boss.oss-cn-qingdao.aliyuncs.com/static',
                name: '[name].[ext]',
                emitFile: false
              }
            }
          ]
        },
        {
          test: /\.less$/,
          exclude: /node_modules/,
          use: [
            {
              loader: mode === 'development' ? 'vue-style-loader' : MiniCssExtractPlugin.loader
            },
            {
              loader: 'css-loader'
            },
            {
              loader: 'less-loader',
              options: {
                paths: [
                  path.resolve(__dirname, `../src/assets/less`)
                ]
              }
            },
            {
              loader: 'postcss-loader',

            }
          ]
        }
      ]
    },
    resolve: {
      alias: {
        '~': path.join(__dirname, `../src/${env.entry}`),
        '@': path.join(__dirname, `../src`),
        vue$: 'vue/dist/vue.esm.js'
      },
      extensions: ['.vue', '.ts', '.js', '.less']
    },
    plugins: [
      new webpack.DefinePlugin({
        'entry': JSON.stringify(env.entry),
        'buildTime': +new Date()
      }),
      new VueLoaderPlugin(),
      new HtmlWebpackPlugin({
        favicon: `./src/assets/static${env.entry}/favicon.ico`,
        filename: 'index.html',
        template: `./src/assets/static${env.entry}/index.html`,
        inject: true,
        minify: {
          removeComments: true,
          collapseWhitespace: true
        }
      }),
      new MiniCssExtractPlugin({
        filename: 'style/[name].css',
        chunkFilename: 'style/[name].css'
      }),
      new HtmlWebpackIncludeAssetsPlugin({
        assets: [
          {
            path: '//cdnjs.cloudflare.com/ajax/libs/element-ui/2.4.0/theme-chalk/index.css',
            type: 'css'
          },
          {
            path: '//at.alicdn.com/t/font_735282_qx191kve0oh.css',
            type: 'css'
          },
          {
            path: '//cdn.jsdelivr.net/npm/v-charts/lib/style.min.css',
            type: 'css'
          }
        ],
        append: false,
        publicPath: ''
      })
    ]

  }, envConfig(env))
}
